import json

import pymysql

from config import abs_path


def assrt(self,result,success,code,message):
    self.assertEqual(200, result.status_code)
    self.assertEqual(success, result.json().get("success"))
    self.assertEqual(code, result.json().get("code"))
    self.assertEqual(message, result.json().get("message"))


class Mysql(object):
    connect=None
    @classmethod
    #连接数据库
    def __connect(cls):
        if cls.connect is None:
            cls.connect=pymysql.connect(port=3306,host="localhost",user="root",password="root",database="test",charset="utf8")
        return cls.connect


    #断开连接
    @classmethod
    def __dis_connect(cls):
        if cls.connect is not None:
            cls.connect.close()
            cls.connect=None

    #数据库查询
    @classmethod
    def search(cls,sql):
        cursor=cls.__connect().cursor()
        cursor.execute(sql)
        result=cursor.fetchall()
        print(result)
        cursor.close()
        cls.__dis_connect()
        return result


    #增删改
    @classmethod
    def zsg(cls,sql):
        cursor=cls.__connect().cursor()
        cursor.execute(sql)
        print(cls.__connect().affected_rows())
        cls.__connect().commit()
        cursor.close()
        cls.__dis_connect()



def get_data(json_data):
    list = []
    with open(json_data, encoding="utf-8") as f:
        data1 = json.load(f)

        for dic1 in data1:
            tuple1 = tuple(dic1.values())
            list.append(tuple1)
    return list
